package com.yuanfud.blog.controller;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.yuanfud.blog.VO.NewsVO;
import com.yuanfud.blog.service.NewsService;
import com.yuanfud.common.exception.RestException;
import com.yuanfud.common.util.DateHandler;
import com.yuanfud.common.util.ErrorCode;
import com.yuanfud.common.util.ResultBody;
import com.yuanfud.entity.blog.ArticleList;
import com.yuanfud.entity.common.MasterPage;
import com.yuanfud.entity.common.PageInfoModel;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class NewsController {
    private static  Logger logger= LoggerFactory.getLogger(NewsController.class);
    @Autowired
    NewsService newsService;

    /**
     * 获取博客文章列表
     * @return
     * @throws RestException
     */
    @RequestMapping( value = "/news/list/all",method = RequestMethod.GET,produces = "application/json")
    public ResultBody<PageInfoModel<ArticleList>> getNewsInfo(MasterPage page) throws RestException {
        System.out.println(page);
        ResultBody<PageInfoModel<ArticleList>> result=new ResultBody<>();
        try {
            PageInfoModel<ArticleList> list=newsService.getNewsInfo(page);
            if(list.getTotalRows()>0) {
                result.setCode(ErrorCode.SUCCESS_OK);
                result.setMsg(ErrorCode.SUCCESS_OK_MSG);
                result.setData(list);
            }
            else
                throw new RestException(ErrorCode.SYS_ERROR,ErrorCode.SYS_ERROR_MSG);
        } catch (Exception e) {
            logger.error("获取博客列表错误："+e.getMessage());
            throw new RestException(ErrorCode.SYS_CONTENT_ERROR,ErrorCode.SYS_CONTENT_ERROR_MSG);
        }
        return result;
    }

    /**
     * 获取博客文章
     * @return
     * @throws RestException
     */
    @RequestMapping( value = "/news/info/{id}",method = RequestMethod.GET,produces = "application/json")
    public ResultBody<ArticleList> getNewsInfoById(@PathVariable("id") int id) throws RestException {
        ResultBody<ArticleList> result=new ResultBody<>();
        try {
            ArticleList list=newsService.getNewsById(id);
            if(list!=null) {
                result.setCode(ErrorCode.SUCCESS_OK);
                result.setMsg(ErrorCode.SUCCESS_OK_MSG);
                result.setData(list);
                
            }
            else
                throw new RestException(ErrorCode.SYS_ERROR,ErrorCode.SYS_ERROR_MSG);
        } catch (RestException e) {
            logger.error("获取博客信息错误："+e.getMessage()+" id="+id);
            throw new RestException(e.getCode(),e.getMessage());
        }
        return result;
    }

    /**
     * 获取博客类别列表
     * @param fatherId
     * @return
     * @throws RestException
     */
    @RequestMapping(value="/news/type/list/{id}",method =RequestMethod.GET )
    public ResultBody<List<Map<Integer,String>>> getBlogTypeList(@PathVariable("id") Integer fatherId) throws RestException {
        ResultBody<List<Map<Integer,String>>> resultBody=new ResultBody<>();

        try {
            List<Map<Integer,String>> list=newsService.getNewsTypeList(fatherId);
            if(list.size()>0) {
                resultBody.setCode(ErrorCode.SUCCESS_OK);
                resultBody.setMsg(ErrorCode.SUCCESS_OK_MSG);
                resultBody.setData(list);
            }
            else
                throw new RestException(ErrorCode.BLOG_ARTICLE_TYPE_LIST_NOT_FOUND_ERROR,ErrorCode.BLOG_ARTICLE_TYPE_LIST_NOT_FOUND_ERROR_MSG);
        } catch (Exception e) {
            logger.error("获取博客类别列表错误："+e.getMessage());
            throw new RestException(ErrorCode.SYS_CONTENT_ERROR,ErrorCode.SYS_CONTENT_ERROR_MSG);
        }
        return resultBody;
    }
    @RequestMapping(value="/article/add",method = RequestMethod.POST,produces = "application/json")
    public ResultBody<Boolean> addNewsInfo(@RequestBody NewsVO news,@RequestHeader("userId") int userId) throws RestException {
        ResultBody<Boolean> resultBody=new ResultBody<>();
        news.setInputUser(userId);
        news.setInputDate(DateHandler.dateToString(new Date(),"yyyy/MM/dd HH:mm:ss"));
        news.setReadNum(0);
        boolean result;
        try {
            result=newsService.addNewsInfo(news);
            if(result) {
                resultBody.setCode(ErrorCode.SUCCESS_OK);
                resultBody.setMsg(ErrorCode.SUCCESS_OK_MSG);
                resultBody.setData(result);
            }
            else
                throw new RestException(ErrorCode.BLOG_ARTICLE_SUBMIT_ERROR,ErrorCode.BLOG_ARTICLE_SUBMIT_ERROR_MSG);
        } catch (Exception e) {
            logger.error("博客提交失败："+e.getMessage());
            throw new RestException(ErrorCode.SYS_CONTENT_ERROR,ErrorCode.SYS_CONTENT_ERROR_MSG);
        }
        return resultBody;
    }
    @RequestMapping(value="/article/edit", method = RequestMethod.POST,produces = "application/json")
    public ResultBody<Boolean> articleEdit(NewsVO news) throws RestException {
        ResultBody<Boolean> resultBody=new ResultBody<Boolean>();
        boolean result;
        result=newsService.ArticleEdit(news);
        try {
            if (result) {
                resultBody.setCode(ErrorCode.SUCCESS_OK);
                resultBody.setMsg(ErrorCode.SUCCESS_OK_MSG);
                resultBody.setData(result);
            } else
                throw new RestException(ErrorCode.BLOG_ARTICLE_SUBMIT_ERROR, ErrorCode.BLOG_ARTICLE_SUBMIT_ERROR_MSG);
        }
        catch (Exception e){
            logger.error("博客更新失败："+e.getMessage());
            throw new RestException(ErrorCode.SYS_CONTENT_ERROR,ErrorCode.SYS_CONTENT_ERROR_MSG);
        }
        return resultBody;
    }

    @RequestMapping(value="/article/del/{id}", method = RequestMethod.GET,produces = "application/json")
    public ResultBody<Boolean> delarticle(@PathVariable("id") int id) throws RestException {
        ResultBody<Boolean> resultBody=new ResultBody<Boolean>();
        boolean result;
        result=newsService.delArticle(id);
        try {
            if (result) {
                resultBody.setCode(ErrorCode.SUCCESS_OK);
                resultBody.setMsg(ErrorCode.SUCCESS_OK_MSG);
                resultBody.setData(result);
            } else
                throw new RestException(ErrorCode.BLOG_ARTICLE_SUBMIT_ERROR, ErrorCode.BLOG_ARTICLE_SUBMIT_ERROR_MSG);
        }
        catch (Exception e){
            logger.error("博客更新失败："+e.getMessage());
            throw new RestException(ErrorCode.SYS_CONTENT_ERROR,ErrorCode.SYS_CONTENT_ERROR_MSG);
        }
        return resultBody;
    }
}
